﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using HRM.Models;
using DTO;
namespace HRM.api
{
    public class VuAPIController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<VU> Get()
        {
            WebData model = new WebData();
            var vus = model.VUs.GetAll();
            return vus;
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
       // public void Post([FromBody]string value)
       // {
       // }

        public HttpResponseMessage Post(VU item) {
            try {
                if (ModelState.IsValid)
                {
                    VU vu = item;// new VU();
                    WebData model = new WebData();
                    model.VUs.InsertOnSubmit(ref item);
                    var response = Request.CreateResponse<VU>(HttpStatusCode.Created, vu);
                    return response;
                }
                else {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid");
                }
            }
            catch(Exception ex) {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
        // PUT api/<controller>/5
      //  public void Put(int id, [FromBody]string value)
       // {
       // }

         public HttpResponseMessage Put(int id,VU item){
             try{
                  WebData model = new WebData();
                 model.VUs.UpdateOnSubmit(item);
             }
             catch(Exception ex){
                   return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
             }
              return Request.CreateResponse(HttpStatusCode.OK);
         }
        // DELETE api/<controller>/5
       // public void Delete(int id)
       // {
       // }

        public HttpResponseMessage Delete(int id) {
            try
            {
                WebData model = new WebData();
                VU item = new VU();
                item.ID = id;
                model.VUs.DeleteOnSubmit(item);
            }
            catch(Exception ex) {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}